package com.wsseieiei.fireballgame.blocks;

import com.wsseieiei.fireballgame.init.BlockEntityInit;
import com.wsseieiei.fireballgame.init.BlockInit;
import net.minecraft.Util;
import net.minecraft.core.BlockPos;
import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.BaseEntityBlock;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.RenderShape;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityTicker;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;

import javax.annotation.Nonnull;
import java.util.Random;

import static com.wsseieiei.fireballgame.init.BlockEntityInit.CORE_BLOCK_ENTITY;

public class Core {
    public static final class CoreB extends BaseEntityBlock {
        public CoreB(Properties props) {
            super(props);
        }
        @Nonnull
        @Override
        public RenderShape getRenderShape(@Nonnull BlockState state) {
            return RenderShape.MODEL;
        }
        @Override
        public BlockEntity newBlockEntity(@Nonnull BlockPos pos, @Nonnull BlockState state) {
            return new CoreEntity(pos, state);
        }
        @Override
        public <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level level, @Nonnull BlockState state, @Nonnull BlockEntityType<T> blockEntityType) {
            return level.isClientSide() ? null : createTickerHelper(blockEntityType, CORE_BLOCK_ENTITY.get(), CoreEntity::tick);
        }
    }
    public static final class CoreEntity extends BlockEntity {
        public CoreEntity(BlockEntityType<CoreEntity> type, BlockPos worldPosition, BlockState blockState) {
            super(type, worldPosition, blockState);
        }
        public CoreEntity(BlockPos worldPosition, BlockState blockState) {
            this(CORE_BLOCK_ENTITY.get(), worldPosition, blockState);
        }
        private static int count = 0;
        public static void tick(Level level, BlockPos pos, BlockState state, CoreEntity entity) {
            count+=1;
            var flag=0;
            final Random random = new Random();
            Player player=level.getNearestPlayer(pos.getX(), pos.getY(), pos.getZ(), 5.0, false);
            if (count > 60) {
                count = 0;
                if(level.getBlockState(new BlockPos(pos.getX()+1,pos.getY(),pos.getZ()+1))== BlockInit.EXAMPLE_BLOCK2.get().defaultBlockState()){
                    if(level.getBlockState(new BlockPos(pos.getX()+1,pos.getY()+1,pos.getZ()+1))== BlockInit.EXAMPLE_BLOCK2.get().defaultBlockState()){
                        if(level.getBlockState(new BlockPos(pos.getX()+1,pos.getY(),pos.getZ()-1))== BlockInit.EXAMPLE_BLOCK2.get().defaultBlockState()){
                            if(level.getBlockState(new BlockPos(pos.getX()+1,pos.getY()+1,pos.getZ()-1))== BlockInit.EXAMPLE_BLOCK2.get().defaultBlockState()){
                                if(level.getBlockState(new BlockPos(pos.getX()-1,pos.getY(),pos.getZ()+1))== BlockInit.EXAMPLE_BLOCK2.get().defaultBlockState()){
                                    if(level.getBlockState(new BlockPos(pos.getX()-1,pos.getY()+1,pos.getZ()+1))== BlockInit.EXAMPLE_BLOCK2.get().defaultBlockState()){
                                        if(level.getBlockState(new BlockPos(pos.getX()-1,pos.getY(),pos.getZ()-1))== BlockInit.EXAMPLE_BLOCK2.get().defaultBlockState()){
                                            if(level.getBlockState(new BlockPos(pos.getX()-1,pos.getY()+1,pos.getZ()-1))== BlockInit.EXAMPLE_BLOCK2.get().defaultBlockState()){

                                                if((level.getBlockState(new BlockPos(pos.getX(),pos.getY()+1,pos.getZ()))== Blocks.PURPUR_BLOCK.defaultBlockState())&&(level.getBlockState(new BlockPos(pos.getX(),pos.getY()+2,pos.getZ()))== Blocks.FIRE.defaultBlockState())){
                                                    level.setBlockAndUpdate(new BlockPos(pos.getX(),pos.getY()+2,pos.getZ()), BlockEntityInit.MY_MACHINE.get().defaultBlockState());
                                                    flag=1;
                                                    count+=1;
                                                }
                                                if((level.getBlockState(new BlockPos(pos.getX(),pos.getY()+1,pos.getZ()))== BlockInit.EXAMPLE_BLOCK.get().defaultBlockState())&&(level.getBlockState(new BlockPos(pos.getX(),pos.getY()+2,pos.getZ()))== Blocks.FIRE.defaultBlockState())){
                                                    level.setBlockAndUpdate(new BlockPos(pos.getX(),pos.getY()+2,pos.getZ()), BlockEntityInit.OC.get().defaultBlockState());
                                                    flag=1;
                                                    count+=1;
                                                }

                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                if (player != null){
                    if(flag==1){
                        player.sendMessage(new TranslatableComponent("chat.fireballgame.core_ok"), Util.NIL_UUID);
                    }

                }
                if(flag==1){
//                    player.playSound( SoundEvents.LAVA_AMBIENT, 2.5F,2.5F);
                    level.removeBlock(new BlockPos(pos.getX(),pos.getY(),pos.getZ()),true);
                    level.removeBlock(new BlockPos(pos.getX(),pos.getY()+1,pos.getZ()),true);
                    level.removeBlock(new BlockPos(pos.getX()+1,pos.getY(),pos.getZ()+1),true);
                    level.removeBlock(new BlockPos(pos.getX()+1,pos.getY()+1,pos.getZ()+1),true);
                    level.removeBlock(new BlockPos(pos.getX()+1,pos.getY(),pos.getZ()-1),true);
                    level.removeBlock(new BlockPos(pos.getX()+1,pos.getY()+1,pos.getZ()-1),true);
                    level.removeBlock(new BlockPos(pos.getX()-1,pos.getY(),pos.getZ()+1),true);
                    level.removeBlock(new BlockPos(pos.getX()-1,pos.getY()+1,pos.getZ()+1),true);
                    level.removeBlock(new BlockPos(pos.getX()-1,pos.getY(),pos.getZ()-1),true);
                    level.removeBlock(new BlockPos(pos.getX()-1,pos.getY()+1,pos.getZ()-1),true);
                }
            }
            if(player!=null){
                if(flag==1){
                    level.getServer().getLevel(level.dimension()).sendParticles(ParticleTypes.SOUL_FIRE_FLAME, pos.getX() + random.nextDouble(1), pos.getY() + 2 + random.nextDouble(0.5d + 100 / 100d, 1.0d + 100 / 100d), pos.getZ() + random.nextDouble(1), 300, 0, 0.0d, 0, 5 / 100d);
                }
                else{
                    level.getServer().getLevel(level.dimension()).sendParticles(ParticleTypes.FLAME, pos.getX() + random.nextDouble(1), pos.getY() + 2 + random.nextDouble(0.5d + count / 100d, 1.0d + count / 100d), pos.getZ() + random.nextDouble(1), 7, 0, 0.0d, 0, 3 * count / 100d);
                }
            }
        }
    }
}
